home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / stk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  1.9 KB  |  96 lines

  1. /*
  2. **  STK.C - Stack manager
  3. **
  4. **  by Dustin Puryear, placed into Public Domain.
  5. */
  6.  
  7.  
  8. #include <stdio.h>       /* General include file */
  9. #include <stdlib.h>      /* malloc() and free()  */
  10. #include "stk.h"         /* Function prototypes  */
  11.  
  12. extern void stkInit(Stk *pstack)
  13. {
  14.       pstack->ptop = NULL;
  15.       pstack->vcount = 0;
  16. }
  17.  
  18. extern int stkPush(Stk *pstack, void *pdata)
  19. {
  20.       StkNode *padd;   /* Node to add to top of stack */
  21.  
  22.       /*
  23.       ** Create the node to append to the top of the stack. Also,
  24.       ** check for a memory allocation error.
  25.       */
  26.  
  27.       padd = (StkNode *) malloc(sizeof(StkNode));
  28.       if ( padd == NULL )
  29.       {
  30.             return (0);
  31.       }
  32.  
  33.       /*
  34.       ** Now, add the data pointer to the node.
  35.       */
  36.  
  37.       padd->pdata = pdata;
  38.  
  39.       /*
  40.       ** Manipulate the stack so that the new node is on top, and
  41.       ** increment count.
  42.       */
  43.  
  44.       if ( pstack->ptop == NULL )
  45.       {
  46.             padd->pprev = NULL;   /* Anchor it!  */
  47.             pstack->ptop = padd;
  48.       }
  49.       else
  50.       {
  51.             padd->pprev = pstack->ptop;
  52.             pstack->ptop = padd;
  53.       }
  54.       pstack->vcount++;
  55.  
  56.       return (1);
  57. }
  58.  
  59. extern int stkPop(void **ppdata, Stk *pstack)
  60. {
  61.       StkNode *pdel;   /* Pointer to node to remove (top) */
  62.  
  63.       /*
  64.       ** Check to see if stack is empty.
  65.       */
  66.  
  67.       if ( pstack == NULL )
  68.       {
  69.             ppdata = NULL;
  70.             return (0);
  71.       }
  72.  
  73.       /*
  74.       ** Pop data pointer value into ppdata (pointer to user's pointer)
  75.       */
  76.  
  77.       *ppdata = pstack->ptop->pdata;
  78.  
  79.       /*
  80.       ** Pop the top of the stack off and make the next in line top.
  81.       ** Also, decrement counter.
  82.       */
  83.  
  84.       pdel = pstack->ptop;
  85.       pstack->ptop = pstack->ptop->pprev;
  86.       free(pdel);
  87.       pstack->vcount--;
  88.  
  89.       return (1);
  90. }
  91.  
  92. extern unsigned stkCount(Stk *pstack)
  93. {
  94.       return (pstack->vcount);
  95. }
  96.